/* unhide.m -- tell an application to unhide itself */
/* This application was written by eps@futon.SFSU.EDU (Eric P. Scott)
 * and posted to the net.
 *
 * This is a good example of why TickleServices needs loadable Tcl
 * built-ins.
 */

/* cc -o unhide -s -O unhide.m -lNeXT_s */
/* cc -arch m68k -arch i386 -o unhide -s -O unhide.m -lNeXT_s */

#ifdef NX_COMPILER_RELEASE_3_0
#include <bsd/libc.h>
#include <mach/mach.h>
#else
#include <libc.h>
#include <mach.h>
#endif
#import <appkit/Listener.h>
#import <appkit/Speaker.h>

main(int argc, char *argv[])
{
    register int i;
    char *a;
    port_t p;
    int e;
    id spkr;

    if (argc<2) {
	(void)fprintf(stderr, "Usage: %s application [...]\n", *argv);
	exit(1);
    }
    e=0;
    spkr=nil;
    for (i=1;i<argc;i++) {
	a=argv[i];
	if (!strcmp(a, "Workspace")) {
	    register char *w;
	    if (w=getenv("Workspace")) a=w;
	}
	if (p=NXPortNameLookup(a, (char *)NULL)) {
	    if (!spkr) spkr=[[Speaker alloc] init];
	    [spkr setSendPort:p];
	    if ([spkr performRemoteMethod:"unhide"]) e++;
	    (void)port_deallocate(task_self(), p);
	}
	else {
	    (void)fprintf(stderr, "%s: \"%s\" not running\n", *argv, argv[i]);
	    e++;
	}
    }
    if (spkr) [spkr free];
    exit(e);
}
